require(['../config/requireconfig'],function(config){
	//根据需要添加
	require(['zepto','common/mcommon','common/validate'],function($,com){
        com.init();
		var pageLogic = new PageLogic($,com);
		pageLogic.init();

	});
});
var PageLogic = function($,com){
	return{
		init:function(){
			var _this = this;
			$("#subForm").prop("action","/saveStoreJoin.json")
			$("#subForm").validate(function(res){
				if(!res.success){
					com.toast("您的数据提交失败");
				}else{
					com.toast("您的数据提交成功",function(){
						window.location.reload();
					});
				}
			},{
				validate:function(){
					if($("#prov").val() == "0" ){
						com.toast("请选择省份");
						return false;
					}
					if($("#city").val() == "0" ){
						com.toast("请选择城市");
						return false;
					}
					var yzm = $("#yzm").val();
					com.ajax({
						async:true,
						url:"/isValidYzm.json",
						data:{yzm:yzm},
						success:function(res){
							if(!res.success){
								com.toast(res.message);
								return false;
							}
						}	
					});
					return true;
				}
			});
			_this.loadStateData();
			//$("#kaptchaImg").prop("src", com.Config.host + "/kaptcha?1");
			$("#kaptchaImg").on("click",function(){
				var url = com.Config.host + "/kaptcha?" + com.utils().randomWord(6); 
				$(this).prop("src", url);
			})
		},
		loadStateData:function(){
			var _this = this;
			com.ajax({
				url:'/allstates.json',
				type:'get',
				success:function(res){
					if(res.success){
						_this.allstates = res.data;
						_this.selectProvince();
					}
				}
			}); 
		},
		selectProvince:function(){
			var _this = this;
			$("#prov").html(_this.mkOption(_this.allstates,"请选择您的省份"));
			$("#city").html(_this.mkOption(null,"请选择您的城市"));
			$("#prov").on("change",function(){
				_this.selectCity($(this).val());
			})
		},
		selectCity:function(code){
			var _this = this;
			if(code == "0"){
				$("#city").html(_this.mkOption(null,"请选择您的城市"));
				return;
			}
			var data = _this.allstates[code].childMap;
			$("#city").html(_this.mkOption(data,"请选择您的城市"));
			
		},
		mkOption:function(data,name){
			var options = '<option value="0">' + name + '</option>';
			if(data){
				for(var key in data){
					options += ' <option value="' +  key + '">' + data[key].name + '</option>'
				}
			}
			return options;
		}
	}
}